"""
    Controller for order table.
    Takes another template for show() method.
    At rds() method replace headers and returns *.rds report text file, not html.
"""

import turbogears, cherrypy
from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.containers import ModelObjectContainer

class OrderController(LookupController, ModelObjectContainer):
    model_object = model.Order
    lookup_columns = [
        TableCol(name='id', caption='#'),
        TableCol(name='creation_date', caption=_("Date")),
        TableCol(name='title', caption=_("Type")),
        TableCol(name='comment', caption=_("Comment"), hidden=True),
        TableCol(name='created_by', caption=_("Created by"), hidden=True),
        TableCol(name='fixed_by', caption=_("Fixed by"), hidden=True),
    ]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('order_lookup'),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
        )

        ModelObjectContainer.__init__(self, self.model_object)

    @turbogears.expose(template="genshi:mnemoply.templates.order_show")
    def show(self, obj):
        return dict(record=obj)
    show.expose_object = True

    @turbogears.expose()
    def rds(self, obj):
        cherrypy.response.headerMap['Content-Type'] = "application/rds"
        cherrypy.response.headerMap['Content-Disposition'] = \
            "filename=\"order-%d.rds\"" % int(obj.id)
        return obj.data
    rds.expose_object = True

exposed = OrderController()
